<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../js/boot.js"></script>
</head>

<body>
    <div class="layui-fluid">
        <div class="flexrow">
            <div class="layui-panel list-left-container" style="width: 200px;">
                <ul id="datatree" class="ztree"></ul>
            </div>
            <div class="layui-panel list-right-container">
                <div class="list-search-container">
                    <button class="layui-btn layui-btn-sm" onclick="HandleAdd()"><i class="layui-icon layui-icon-add-1 layui-font-12"></i>新增部门</button>
                    <button class="layui-btn layui-btn-sm" onclick="ExportData()"><i class="layui-icon layui-icon-download-circle layui-font-12"></i>导出数据</button>
                    <button class="layui-btn layui-btn-sm" onclick="ImportData()"><i class="layui-icon layui-icon-upload-circle layui-font-12"></i>导入数据</button>
                    <button class="layui-btn layui-btn-sm" onclick="BatchUpdate()"><i class="layui-icon layui-icon-ok layui-font-12"></i>保存修改</button>
                    <div style="display: none">
                        <input type="file" name="file" id="file" onchange="uploadXml()" accept=".xml" />
                    </div>
                    <div class="list-search-item">
                        <span>部门名称：</span>
                        <input id="dept_name" autocomplete="off" class="layui-input">
                    </div>
                    <button class="layui-btn layui-btn-sm" style="margin-left:5px" onclick="BindData()"><i class="layui-icon layui-icon-search layui-font-12"></i>搜索</button>
                </div>
                <table class="layui-table" lay-data="{id: 'datagrid'}" lay-filter="datagrid">
                    <thead>
                        <tr>
                            <th lay-data="{type:'checkbox'}"></th>
                            <th lay-data="{field:'dept_name'}">部门名称</th>
                            <th lay-data="{field:'office_phone',width:150,align:'center'}">办公电话</th>
                            <th lay-data="{field:'code',width:100,align:'center'}">部门编码</th>
                            <th lay-data="{field:'sort_num',width:80,align:'center',edit:'text'}">排序</th>
                            <th lay-data="{fixed: 'right', width:175, align:'center', toolbar: '#op'}">操作</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</body>
<script type="text/html" id="op">
    <button class="layui-btn layui-btn-sm" lay-event="HandleEdit"><i class="layui-icon layui-icon-edit layui-font-12"></i>编辑</button>
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="HandleDelete"><i class="layui-icon layui-icon-delete layui-font-12"></i>删除</button>
</script>
<script>
    var parent_id = "Top";
    var TableData;
    $(function() {
        BindData();
    })

    function BindData() {
        BindTableData();
        BindTreeData();
    }

    function BindTableData() {
        var param = {
            dept_name: $("#dept_name").val(),
            parent_id: parent_id
        }
        BindTable("datagrid", "system/org/dept/list", param, function (res) {
            TableData = res.data;
            BindTableCellEdit('datagrid',function(UpdateTableData){
                TableData = UpdateTableData;
            })
        });
    }

    function BindTreeData() {
        BindTree("datatree", "system/org/dept/tree", null, TreeNodeClick);
    }

    function TreeNodeClick(event, treeId, treeNode, clickFlag) {
        parent_id = treeNode.id;
        BindTableData();
    }

    function HandleAdd() {
        OpenDialog('新增部门', 'system/dept/dept_add.html?parent_id=' + parent_id, 900, 750, function() {
            BindData();
        })
    }

    function HandleEdit(RowData) {
        OpenDialog('修改部门', 'system/dept/dept_edit.html?row_id=' + RowData.row_id, 900, 750, function() {
            BindData();
        })
    }

    function HandleDelete(RowData) {
        OpenConfirm('确认删除？', function() {
            var param = {
                row_id: RowData.row_id
            }
            HttpPost(JSON.stringify(param), 'system/org/dept/delete', function(result) {
                if (result.code == 0) {
                    OpenSuccessMessage("删除成功")
                    BindData();
                } else {
                    OpenFail(result.msg);
                }
            })
        })
    }

    function ExportData() {
        var checkData = GetCheckData();
        if (checkData.length > 0) {
            StartLoading("导出中");
            var rowIDList = [];
            checkData.forEach(element => {
                rowIDList.push(element.row_id)
            });
            HttpPost(JSON.stringify(rowIDList), 'system/org/dept/exportData', function(result) {
                CloseLoading();
                if (result.code == 0) {
                    window.top.open(GetApiPath() + "/system/common/attach/readExportXmlFile?fileid=" + result.data.fileid + "&token=" + GetTokenID())
                } else {
                    OpenFail(result.msg);
                }
            })
        } else {
            OpenWarning("未选择数据")
        }
    }

    function ImportData() {
        document.getElementById("file").click();
    }

    function uploadXml() {
        StartLoading("导入中");
        var formData = new FormData();
        formData.append('file', document.getElementById('file').files[0]);
        HttpForm(formData, 'system/org/dept/importData', function(result, status) {
            CloseLoading();
            if (result.code == 0) {
                OpenSuccess('导入成功', function() {
                    BindData();
                })
            } else {
                OpenFail(result.msg);
            }
            document.getElementById("file").value = "";
        })
    }

    function BatchUpdate(){
        HttpPost(JSON.stringify(TableData), "system/org/dept/batchUpdate", function (result, status) {
            if (result.code == 0) {
                OpenSuccessMessage("保存成功");
                BindTableData();
            } else {
                OpenFail(result.msg)
            }
        })
    }
</script>

</html>